﻿<template>
	<div class="whiteList-container">
		<el-card shadow="hover" >
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label-width='70' label="手机号码" prop="phone">
					<el-input placeholder="手机号码" clearable class='ml10' @keyup.enter="handleQuery" v-model="state.queryParams.phone" />
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'whiteList:query'"> 查询 </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery" v-auth="'whiteList:query'"> 重置 </el-button>
					</el-button-group>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card shadow="hover" style="margin-top: 8px">
            <div style="margin: -8px 0 10px 0;">
                <el-button type="success" plain icon="ele-Plus" @click="openAddWhiteList" v-auth="'whiteList:add'"> 新增 </el-button>
            </div>
			<el-table
				:data="state.data"
				style="width: 100%"
				v-loading="state.loading"
				tooltip-effect="light"
				row-key="id"
				border stripe highlight-current-row>
				<el-table-column header-align="center" type="index" label="序号" width="55" align="center" />
				<el-table-column header-align="center" prop="phone" label="手机号码" show-overflow-tooltip />
				<el-table-column header-align="center" prop="createTime" label="创建时间" show-overflow-tooltip />
				<el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip >
					<template #default="scope">
						<el-button icon="ele-Delete" size="small" text type="danger" @click="delWhiteList(scope.row)" v-auth="'whiteList:delete'"> 删除 </el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
				:page-sizes="[20, 50, 100]" v-model:current-page="state.queryParams.pageNum" background
				v-model:page-size="state.queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="state.total" />
			<EditWhiteList ref="editWhiteListRef" :title="state.editTitle" />
		</el-card>
	</div>
</template>

<script lang="ts" setup name="whiteList">
import { onMounted, onUnmounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import EditWhiteList from '/@/views/common/whiteList/component/editWhiteList.vue';

import { WhitePhone } from '/@/api/models';
import SysWhiteListApi from '/@/api/common/whiteList';

// 定义变量内容
const editWhiteListRef = ref<InstanceType<typeof EditWhiteList>>();
const state = reactive({
	loading: false,
	data: [] as Array<WhitePhone>,
	total: 0 as any,
	queryParams: {
		phone: undefined,
		pageNum: 1,
		pageSize: 20,
	},
	editTitle: '',
});

onMounted(async () => {
	mittBus.on('submitRefresh', () => {
		handleQuery();
	});
});

onUnmounted(() => {
	mittBus.off('submitRefresh');
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	const res = await SysWhiteListApi().queryRecord(state.queryParams);
	state.data = res.data.result.items ?? [];
	state.total = res.data.result?.total;
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.phone = undefined;
	handleQuery();
};

// 打开新增页面
const openAddWhiteList = () => {
	state.editTitle = '添加';
	editWhiteListRef.value?.openDialog({ });
};

// 删除
const delWhiteList = (row: any) => {
	ElMessageBox.confirm(`确定删除：【${row.phone}】?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
	.then(async () => {
		let res = await SysWhiteListApi().deletePost({ phone: row.phone });
		if (res.data.resCode != '0000')
			ElMessage.error(res.data.resMessage);
		else
			ElMessage.success("删除成功");
		await handleQuery();
	})
	.catch(() => {});
};

// 改变页面容量
const onHandleSizeChange = (val: number) => {
	state.queryParams.pageSize = val;
	handleQuery();
};

// 改变页码序号
const onHandleCurrentChange = (val: number) => {
	state.queryParams.pageNum = val;
	handleQuery();
};

</script>
